using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Rss;
using System.Text;
using System.Text.RegularExpressions;


namespace LWRSSReader
{
	static class Program
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main()
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);

			try
			{
				_settings = (Settings) UserSettings.RestoreObject(global::LWRSSReader.Properties.Resources.STR_SETTINGS_FILENAME);
				Console.WriteLine(_settings.Feeds.Count);
			}
			catch
			{
				_settings = new Settings();
			}
			if (_settings == null)
			{
				_settings = new Settings();
			}

			



			f = new Form();
			f.Activated += new EventHandler(f_Activated);
			f.Load += new EventHandler(f_Load);
			Application.ApplicationExit += new EventHandler(Application_ApplicationExit);

			Application.Run(f);
			


		}

		static void Application_ApplicationExit(object sender, EventArgs e)
		{
			UserSettings.StoreObject(global::LWRSSReader.Properties.Resources.STR_SETTINGS_FILENAME, _settings);	
		}




		static Form f;
		private static Form _currentForm;
		private static Form _oldForm;
		static void f_Load(object sender, EventArgs e)
		{
			_currentForm = new Form4();
			_currentForm.Show();
			_currentForm.FormClosed += new FormClosedEventHandler(_currentForm_FormClosed);

		}

		static void _currentForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			if (_oldForm != null)
				_oldForm.Close();

			

			f.Close();



		}

		static void f_Activated(object sender, EventArgs e)
		{
			((Form) sender).Visible = false;
		}



		public static void SwitchUI()
		{
			Form _tmp;
			_tmp = _oldForm;
			_oldForm = _currentForm;
			if (_tmp == null)
			{
				if (_oldForm.GetType().Name == "Form1")
				{
					_tmp = new Form4();
				}
				else
				{
					_tmp = new Form1();
				}
				//_tmp.FormClosed += new FormClosedEventHandler(_currentForm_FormClosed);
				//_tmp.VisibleChanged += new EventHandler(_currentForm_VisibleChanged);
			}
			_currentForm = _tmp;
			_currentForm.Show();
			_oldForm.Visible = false;




		}



		
		private static Settings _settings;
		public static Settings Settings
		{
			get
			{
				return _settings;
			}
		}



		public static void SetCurrentImage(string url, PictureBox pictureBox)
		{
			SetCurrentImage(new Uri(url, UriKind.RelativeOrAbsolute), pictureBox);
		}

		public static void SetCurrentImage(Uri uri, PictureBox pictureBox)
		{

			//lwPictureBox1.Image = lwPictureBox1.InitialImage;
			pictureBox.ImageLocation = uri.AbsoluteUri;

		}
		
		public static void DisplayCurrentMedia(RssItemInfoStruct rssItemInfoStruct)
		{
			//DisplayCurrentMedia(rssItemInfoStruct, false, lwPictureBox1, lwLinkLabel1);
		}
		
		public static void DisplayCurrentMedia(RssItemInfoStruct rssItemInfoStruct, bool changeText, PictureBox pictureBox, LinkLabel linkLabel)
		{


			if (Settings.Feeds [rssItemInfoStruct.feedId] == null || Settings.Feeds [rssItemInfoStruct.feedId].Channels [rssItemInfoStruct.channelId] == null || Settings.Feeds [rssItemInfoStruct.feedId].Channels [rssItemInfoStruct.channelId].Items [rssItemInfoStruct.itemId] == null)
			{
				return;
			}
			RssItem rssItem = Settings.Feeds [rssItemInfoStruct.feedId].Channels [rssItemInfoStruct.channelId].Items [rssItemInfoStruct.itemId];
			RssChannel rssChannel = Settings.Feeds [rssItemInfoStruct.feedId].Channels [rssItemInfoStruct.channelId];
			RssFeedWrap rssFeedWrap = Settings.Feeds [rssItemInfoStruct.feedId];



			if (changeText && rssItem.Link != null)
			{
				linkLabel.Text = StripTags(rssItem.Title);
				linkLabel.Links.Clear();
				linkLabel.Links.Add(0, linkLabel.Text.Length, rssItem.Link);
				//lwLinkLabel1.Links.Add(5, 2, "www.yahoo.com");

			}


			if (rssItem.Enclosure != null)
			{

				Program.SetCurrentImage(rssItem.Enclosure.Url, pictureBox);
				//Console.WriteLine(" {0} {1}", rssItemInfoStruct.itemId, rssItem.Enclosure.Url.AbsoluteUri);

			}
			else
				if (rssChannel.Image != null)
				{
					Program.SetCurrentImage(rssChannel.Image.Url, pictureBox);
				}
				else
				{
					Program.SetCurrentImage(new Uri(String.Empty), pictureBox);
				}






		}


		public static void DisplayCurrentMedia(RssItemInfoStruct rssItemInfoStruct, bool changeText, TextBox textBox)
		{


			if (Settings.Feeds [rssItemInfoStruct.feedId] == null || Settings.Feeds [rssItemInfoStruct.feedId].Channels [rssItemInfoStruct.channelId] == null || Settings.Feeds [rssItemInfoStruct.feedId].Channels [rssItemInfoStruct.channelId].Items [rssItemInfoStruct.itemId] == null)
			{
				return;
			}
			RssItem rssItem = Settings.Feeds [rssItemInfoStruct.feedId].Channels [rssItemInfoStruct.channelId].Items [rssItemInfoStruct.itemId];
			RssChannel rssChannel = Settings.Feeds [rssItemInfoStruct.feedId].Channels [rssItemInfoStruct.channelId];
			RssFeedWrap rssFeedWrap = Settings.Feeds [rssItemInfoStruct.feedId];




			if (changeText && rssItem.Link != null)
			{
				textBox.Text = "";
				//linkLabel.Text = rssItem.Title;
				//linkLabel.Links.Clear();
				//linkLabel.Links.Add(0, linkLabel.Text.Length, rssItem.Link);
				//lwLinkLabel1.Links.Add(5, 2, "www.yahoo.com");

				textBox.Text += StripTags(rssItem.Title) + Environment.NewLine;
				textBox.Text += rssItem.PubDate + Environment.NewLine + Environment.NewLine;
				textBox.Text += StripTags(rssItem.Description) + Environment.NewLine;

			}
			/*

			if (rssItem.Enclosure != null)
			{

				//Program.SetCurrentImage(rssItem.Enclosure.Url, pictureBox);
				//Console.WriteLine(" {0} {1}", rssItemInfoStruct.itemId, rssItem.Enclosure.Url.AbsoluteUri);

			}
			else
				if (rssChannel.Image != null)
				{
				//	Program.SetCurrentImage(rssChannel.Image.Url, pictureBox);
				}
				else
				{
				//	Program.SetCurrentImage(new Uri(String.Empty), pictureBox);
				}

								  */




		}

		public static void UpdateItemList(RssFeedWrapCollection feeds, ListView listView)
		{

			listView.Items.Clear();
			listView.Groups.Clear();


			for (int feedIndex = 0; feedIndex < feeds.Count; feedIndex++)
			{
				RssFeedWrap rssFeedWrap = feeds [feedIndex];
				RssChannel rssChannel = rssFeedWrap.RssSelectedChannel;
				if (rssChannel == null)
					continue;

				ListViewGroup lvg = new ListViewGroup(rssChannel.Title);
				listView.Groups.Add(lvg);

				for (int itemIndex = 0; itemIndex < rssChannel.Items.Count; itemIndex++)
				{
					RssItem rssItem = rssChannel.Items [itemIndex];
					ListViewItem lvi = new ListViewItem(StripTags(rssItem.Title));
					lvi.Tag = new RssItemInfoStruct(itemIndex, rssFeedWrap.RssSelectedChannelId, feedIndex);
					lvi.SubItems.Add(StripTags(rssItem.Description));
					lvi.SubItems.Add(rssItem.PubDate.ToLongDateString());
					lvi.SubItems.Add(rssItem.PubDate.ToLongTimeString());
					listView.Items.Add(lvi);
					lvg.Items.Add(lvi);
				}
			}
		}



		
		static Regex _reg0 = new Regex("<.*?>", RegexOptions.Compiled);
		static Regex _reg1 = new Regex(@"\&nbsp;", RegexOptions.Compiled);
		static Regex _reg2 = new Regex(@"\&quot;", RegexOptions.Compiled);
		public static string StripTags(string text)
		{

			text= _reg0.Replace(text, string.Empty);
			text = _reg1.Replace(text, " ");
			text = _reg2.Replace(text, @"""");
			return text;
		}




	}
}